package cn.mygweb.web.controller;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.mygweb.common.tasks.AsyncTask;

/** 
* <p>Title: TestAsyncController.java</p>
* <p>Description: 测试异步调用程序</p>  
* @author gxb 
* @date 2019年5月23日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@RestController
@RequestMapping("/async")
public class TestAsyncController {

	@Autowired
	private AsyncTask task;
	
	/**
	 * <p>Title: testAsync</p>
	 * <p>Description: 测试异步调用任务</p>
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/testAsync")
	public String testAsync() throws Exception {
		long start = System.currentTimeMillis();
		Future<Boolean> t1 = task.doTask1();
		Future<Boolean> t2 = task.doTask2();
		Future<Boolean> t3 = task.doTask3();
		
		while(!t1.isDone() || ! t2.isDone() || !t3.isDone()) {
			if(t1.isDone() && t2.isDone() && t3.isDone()) {
				break;
			}
		}
		
		long end = System.currentTimeMillis();
		
		String result = "任务全部完成，耗时" + (end - start) + "毫秒";
		System.out.println(result);
		return result;
	}
	
}
